package com.xjy.system.utils;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 通用方法工具类
 *
 * @Author: hlj
 * @Date: 2021/3/19 9:11
 */

public class GeneralUtils {

    private static long tmpID = 0;
    private static boolean tmpIDlocked = false;

    /**
     * 获取token
     *
     * @param request
     * @return
     */
//    public static String getAuthToken(HttpServletRequest request) {
//        String token = request.getHeader(CommonConstant.TOKEN_NAME);
//        if (StringUtils.isEmpty(token)) {
//            token = request.getParameter(CommonConstant.TOKEN_NAME);
//        }
//        return token;
//    }

    /**
     * 生成n位随机码
     *
     * @param place
     * @return
     */
    public static String randomGen(int place) {
        String base = "qwertyuioplkjhgfdsazxcvbnmQAZWSXEDCRFVTGBYHNUJMIKLOP0123456789";
        StringBuffer sb = new StringBuffer();
        Random rd = new Random();
        for (int i = 0; i < place; i++) {
            sb.append(base.charAt(rd.nextInt(base.length())));
        }
        return sb.toString();
    }

    /**
     * 生成随机验证码
     *
     * @return
     */
    public static String getCode() {
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 6; i++) {
            code += random.nextInt(9) + 1;
        }
        return code;
    }

    /**
     * 生成16位随机数
     *
     * @return
     */
    public static Long getId(Boolean is18) {
        long ltime = 0;
        while (true) {
            if (tmpIDlocked == false) {
                tmpIDlocked = true;
                //当前：（年、月、日、时、分、秒、毫秒）*10000
                ltime = Long.valueOf(new SimpleDateFormat("yyMMddHHmmssSS").format(new Date()).toString()) * 100;
                if(is18){
                    ltime = ltime * 100;
                }
                if (tmpID < ltime) {
                    tmpID = ltime;
                } else {
                    tmpID = tmpID + 1;
                    ltime = tmpID;
                }
                tmpIDlocked = false;
                return ltime;
            }
        }
    }

}
